home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / colton / fileio.c < prev    next >
C/C++ Source or Header  |  1995-02-21  |  2KB  |  100 lines

  1. /*
  2.  * Listing 5 - fileio.c (libips.a)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "ips_image.h"
  7.  
  8. int img_to_ips();
  9. int img_to_ips_fp();
  10. int ips_to_img();
  11. int ips_to_img_fp();
  12.  
  13.  
  14. int
  15. img_to_ips(header, image, filename)
  16. ips_header    *header;
  17. ips_image    *image;
  18. char        *filename;
  19. {
  20.     int        retval;
  21.     FILE    *fp;
  22.  
  23.  
  24.     if((fp = fopen(filename, "w")) == NULL) {
  25.         perror("img_to_ips");
  26.         return 0;
  27.     }
  28.  
  29.     retval = img_to_ips_fp(header, image, fp);
  30.  
  31.     fclose(fp);
  32.  
  33.     return retval;
  34. }
  35.     
  36.  
  37. int
  38. img_to_ips_fp(header, image, fp)
  39. ips_header    *header;
  40. ips_image    *image;
  41. FILE        *fp;
  42. {
  43.     fwrite(header, sizeof(ips_header), 1, fp);
  44.     fwrite(image->cmap, 1, sizeof(XColor) * header->cmap_count, fp);
  45.     fwrite(image->data, 1, header->width * header->height, fp);
  46.  
  47.     return 1;
  48. }
  49.  
  50.  
  51. int
  52. ips_to_img(header, image, filename)
  53. ips_header    *header;
  54. ips_image    *image;
  55. char        *filename;
  56. {
  57.     int        retval;
  58.     FILE    *fp;
  59.  
  60.  
  61.     if((fp = fopen(filename, "r")) == NULL) {
  62.         perror("ips_to_img");
  63.         return 0;
  64.     }
  65.  
  66.     retval = ips_to_img_fp(header, image, fp);
  67.  
  68.     fclose(fp);
  69.  
  70.     return retval;
  71. }
  72.  
  73.  
  74. int
  75. ips_to_img_fp(header, image, fp)
  76. ips_header    *header;
  77. ips_image    *image;
  78. FILE        *fp;
  79. {
  80.     fread(header, sizeof(ips_header), 1, fp);
  81.  
  82.     if(header->bpp != 8) {
  83.         fprintf(stderr, "ips_to_img_fp: data is not in 8 bit format.\n");
  84.         return 0;
  85.     }
  86.  
  87.     image->data = (unsigned char *)
  88.          malloc(header->width * header->height);
  89.  
  90.     if(image->data == NULL) {
  91.         perror("ips_to_img_fp");
  92.         return 0;
  93.     }
  94.  
  95.     fread(image->cmap, 1, sizeof(XColor) * header->cmap_count, fp);
  96.     fread(image->data, 1, header->width * header->height, fp);
  97.     
  98.     return 1;
  99. }
  100.